﻿/**
 *
 *	@author	Valentyn Derkach
 *
 *	2008.06.15
 *	Changes:
 *	 -	added state variable
 *	 -	added constants STATE_HIDE, STATE_SHOW
 *
 */

package com.bizmlib.ui.composite
{
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	
	[Event(name="showComplete", type="com.bizmlib.ui.composite.UICompositemItemEvent")]
	[Event(name="showStart", type="com.bizmlib.ui.composite.UICompositemItemEvent")]
	[Event(name="hideComplete", type="com.bizmlib.ui.composite.UICompositemItemEvent")]
	[Event(name="hideStart", type="com.bizmlib.ui.composite.UICompositemItemEvent")]
	
	
	public class UICompositeItem extends MovieClip implements IUICompositeItem
	{
		protected static const STATE_HIDE:int = 1;
		protected static const STATE_SHOW:int = 2;
		
		protected var state:int;
		protected var showDone:Boolean;
		protected var hideDone:Boolean;
		
	
		public function UICompositeItem()
		{
			if(Class(getDefinitionByName(getQualifiedClassName(this)))==UICompositeItem)
			{
				throw new Error(getQualifiedClassName(this) + " can't be instantiated, you should extend it.");
			}
			init();
		}
	
	
		public function show():Boolean
		{
			if(state==STATE_SHOW)
			{
				return false;
			}
			state = STATE_SHOW;
			showDone = false;
			hideDone = false;
			dispatchEvent(new UICompositeItemEvent(UICompositeItemEvent.SHOW_START));
			showStart();
			
			return true;
		}
		public function hide():Boolean
		{
			//trace(this + ".hide(" + arguments + ")");
			
			if(state==STATE_HIDE)
			{
				return false;
			}
			state = STATE_HIDE;
			showDone = false;
			hideDone = false;
			dispatchEvent(new UICompositeItemEvent(UICompositeItemEvent.HIDE_START));
			hideStart();
			
			return true;
		}
	
	
		protected function init():void
		{
			//trace(this + ".init(" + arguments + ")");
			
			state = STATE_HIDE;
			showDone = false;
			hideDone = true;
		}
		protected function showStart():void
		{
			showComplete();
		}
		protected function showComplete():void
		{
			//trace(this + ".showComplete(" + arguments + ")");
			
			showDone = true;
			dispatchEvent(new UICompositeItemEvent(UICompositeItemEvent.SHOW_COMPLETE));
		}
		protected function hideStart():void
		{
			//trace(this + ".hideStart(" + arguments + ")");
			hideComplete();
		}
		protected function hideComplete():void
		{
			//trace(this + ".hideComplete(" + arguments + ")");
			
			hideDone = true;
			dispatchEvent(new UICompositeItemEvent(UICompositeItemEvent.HIDE_COMPLETE));
		}
		
		
		/**
		 * Returns a string describing the instance. Override it.
		 */
		override public function toString():String
		{
			return "[UICompositeItem]";
		}
	}
}